package com.remist.dao.impl.jpa;

import java.util.List;

import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.remist.dao.ImagesDao;
import com.remist.model.ImageMapBean;

public class ImagesDaoImpl  extends JpaDaoSupport implements ImagesDao {

	public ImageMapBean addImage(ImageMapBean image) {
		getJpaTemplate() .persist(image);
		return image;
	}

	public void deleteImageById(ImageMapBean image) {
		ImageMapBean imageMapBean = getJpaTemplate().find(ImageMapBean.class, image.getId());
        if (imageMapBean != null)
        {
            getJpaTemplate().remove(imageMapBean);
        } 
	}

	public ImageMapBean getImageById(ImageMapBean image) {
		return getJpaTemplate().find(ImageMapBean.class, image.getId()); 
	}

	public List<ImageMapBean> getImagesByReferenceId(ImageMapBean image) {
		List<ImageMapBean> find = getJpaTemplate().find(" SELECT  p FROM ImageMapBean p WHERE p.referenceId="+image.getReferenceId());
		return find;
	}

	public void deleteImageByReferenceId(ImageMapBean image) {
		List<ImageMapBean> find =getImagesByReferenceId(image);
        if ( find != null )
        {
        	for(int i=0;i<find.size();i++){
        		ImageMapBean removeBean = find.get(i);
        		getJpaTemplate().remove( removeBean );
        	}
        } 
	}

	public void updateImage(ImageMapBean image) {
		getJpaTemplate().merge(image);
	}

}
